// pages/receiptCode/receiptCode.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl:'',
    userInfo: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    wx:wx.getStorage({
      key: 'personalInfo',
      success: function(res) {
        that.setData({
          userInfo: JSON.parse(wx.getStorageSync('userInfo')),
          imgUrl:res.data.receiptCode
        })
      }
    })
  },
  saveCode:function(e){
    if (this.data.imgUrl!=''){
        wx.navigateTo({
          url: "/pages/carInfo/carInfo?imgUrl=" + this.data.imgUrl
        })
    }
  },
  uploadImg: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      // sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        let key = e.currentTarget.dataset.key;
        var tempFilePaths = res.tempFilePaths;
        that.upload(tempFilePaths);

      }
    })
  },
  /*上传图片*/
  upload: function (path) {
    var that = this;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    }),
      wx.uploadFile({
      // url: app.globalData.httpApiSfc + 'assets/uploadFile',
      url: app.globalData.httpApiCar + 'uploadfile/upload1',
        filePath: path[0],
        name: 'file',
        header: { "Content-Type": "multipart/form-data" },
        formData: {},
        success: function (res) {
          if (res.statusCode != 200) {
            wx.showModal({
              title: '提示',
              content: '上传',
              showCancel: false
            })
            return;
          }
          var url = JSON.parse(res.data).data;
          that.setData({
            'imgUrl': app.globalData.uploadUrl + url
          })
        },
        fail: function (e) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
        },
        complete: function () {
          wx.hideToast();  //隐藏Toast
        }
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})